package net.pimpas.core.visitor;

/**
 * 
 * @author pc
 * Create a sub-class of this class to visit files int ClassPath on BootStrap.
*/

import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import net.pimpas.util.EqualsHelper;

import org.apache.commons.lang.builder.HashCodeBuilder;

public abstract class BaseFileVisitor implements FileVisitor {

	private Collection<String> allowedDirectories;
	
	public BaseFileVisitor() {
		this.setAllowedDirectories(new HashSet<String>());
	}
	
	public BaseFileVisitor(String... directories) {
		this();
		this.getAllowedDirectories().addAll(Arrays.asList(directories));
	}
	
	public boolean accept(URL file) {
		if(this.getAllowedDirectories().size() == 0) return true;
		for(String url: this.getAllowedDirectories()) {
			if(file.getQuery() != null && file.getQuery().equals(url)) return true;
		}
		
		return false;
	}

	public Collection<String> getAllowedDirectories() {
		return allowedDirectories;
	}

	public void setAllowedDirectories(Collection<String> allowedDirectories) {
		this.allowedDirectories = allowedDirectories;
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.getAllowedDirectories()).toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		return EqualsHelper.equals(this, obj);
	}
}